package site.kexing.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import site.kexing.config.MybaitsUtils;
import site.kexing.mapper.ProductMapper;
import site.kexing.mapper.Product_OrderMapper;
import site.kexing.mapper.Product_PlanMapper;
import site.kexing.pojo.Product_Order;
import site.kexing.pojo.Product_Plan;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class Product_PlanController {
    @Autowired
    private Product_OrderMapper product_orderMapper;
    @Autowired
    private Product_PlanMapper product_planMapper;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping("/selectallproductplan")
    public String selectAllProduct_Plan(@RequestParam(defaultValue = "1") Integer currentPage, Model model){
        PageHelper.startPage(currentPage, 7);
        List<Product_Plan> product_plans = product_planMapper.selectAllProduct_Plan();
        PageInfo<Product_Plan> pageInfo = new PageInfo<Product_Plan>(product_plans,7);
        model.addAttribute("pageInfo",pageInfo);
        return "product_plan";
    }

    @RequestMapping("/selectproductplanbyid")
    public String selectProduct_PlanByID(Integer id, Model model){
        Product_Plan product_plan = product_planMapper.selectProduct_PlanByID(id);
        model.addAttribute("product_plan",product_plan);
        return "product_planUpdate";
    }

    @ResponseBody
    @RequestMapping("/choseidcount")
    public String chose_id_count(Integer plan_id) {
        Product_Plan product_plan = product_planMapper.selectProduct_PlanByID(plan_id);
        String s = JSON.toJSONString(product_plan);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/updateproductplan")
    public String updateProduct_Plan(Product_Plan product_plan){
        product_plan.setUpdate_time(new Date());
        product_planMapper.updateProduct_Plan(product_plan);
        if(product_plan.getPlan_status() == 20){
            int i = product_orderMapper.updateProduct_order_status(product_plan.getOrder_id());
        }
        return "redirect:/selectallproductplan";
    }

    @RequestMapping("/deleteproductplan")
    public String deleteProduct_Plan(Integer id){
        int i = product_planMapper.deleteProduct_Plan(id);
        return "redirect:/selectallproductplan";
    }

    @RequestMapping("/toproductplaninsert")
    public String toProduct_planInsert(Model model){
        List<Integer> productOrderIDs = product_orderMapper.selectAllProduct_OrderID();
        model.addAttribute("productOrderIDs",productOrderIDs);
        return "product_planInsert";
    }

    @RequestMapping("/insertproductplan")
    public String insertProduct_Plan(Product_Plan product_plan,Model model){
        product_plan.setCreate_time(new Date());
        product_plan.setUpdate_time(new Date());
        product_plan.setPlan_seq(MybaitsUtils.getUUID());
        product_plan.setProduct_id(product_orderMapper.selectProduct_OrderByID(product_plan.getOrder_id()).getProduct_id());
        product_planMapper.insertProduct_Plan(product_plan);
        if(product_plan.getPlan_status() == 20){
            int i = product_orderMapper.updateProduct_order_status(product_plan.getOrder_id());
        }
        return "redirect:/selectallproductplan";
    }

    @RequestMapping("/selectproductplanbyorderid")
    public String selectProduct_PlanByOrderID(@RequestParam(defaultValue = "1") Integer currentPage,String order_id,Model model){
        PageHelper.startPage(currentPage, 7);
        Integer id = 0;
        if(order_id==null || order_id==""){
            id = null;
        }else{
            id = Integer.parseInt(order_id);
        }
        List<Product_Plan> product_plans = product_planMapper.selectProduct_PlanByOrderID(id);
        PageInfo<Product_Plan> pageInfo = new PageInfo<Product_Plan>(product_plans,7);
        model.addAttribute("pageInfo",pageInfo);
        return "product_plan";
    }
}
